﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSS_XNA.Engine.Script;
using DSS_XNA.Engine.Error;
using DSS_XNA.Engine.World;
using Microsoft.Xna.Framework.Graphics;
using DSS_XNA.Engine.System;

namespace DSS_XNA.Engine.Script.Commands
{
    public class ScriptCommand_SetSystemVariable
    {
        #region Static Methods

        public static void Action(Dictionary<string, string> args, ScriptManager manager, Game1 engine)
        {
            if (!args.ContainsKey("name"))
                throw new DSSException("No Name parameter for SetSystemVariable");
            if (!args.ContainsKey("value"))
                throw new DSSException("No Value parameter for SetSystemVariable");

            string name = manager.GetParameterValue(args["name"]).ToString();
            string value = manager.GetParameterValue(args["value"]).ToString();

            SetSystemVariable(name, value, engine);
        }

        private static void SetSystemVariable(string name, string value, Game1 engine)
        {
            name = name.ToLower().Trim();

            switch (name)
            {
                #region Development
                case "development_isdebugmode": SetIsDebugModeValue(value, engine); break;
                case "development_showdeveloperwindow": SetShowDeveloperWindow(value, engine); break;
                #endregion
                #region Font
                case "font_name": SetFontName(value, engine); break;
                #endregion
                #region Size
                case "size_meter": SetMeterValue(value, engine); break;
                #endregion
                #region Physics
                case "physics_gravity": SetGravityValue(value, engine); break;
                #endregion
                #region Menu
                case "menu_default_script_name": Globals.DefaultMenuScriptName = value; break;
                #endregion
                default: throw new DSSException(String.Format("Unknown system variable: {0}", name));
            }
        }

        private static void SetIsDebugModeValue(string value, Game1 engine)
        {
            Game1.IsDebugMode = Convert.ToBoolean(value);
        }

        private static void SetShowDeveloperWindow(string value, Game1 engine)
        {
            bool show = Convert.ToBoolean(value);

            if(engine.DeveloperWindow == null)
                engine.DeveloperWindow = new DSS_XNA.DeveloperWindow.frmDeveloperWindow(engine);

            if(show)
            {
                engine.DeveloperWindow.Show();
                engine.DeveloperWindow.BringToFront();
            }
            else
            {
                engine.DeveloperWindow.Close();
                engine.DeveloperWindow.Dispose();
                engine.DeveloperWindow = null;
            }
        }

        private static void SetFontName(string value, Game1 engine)
        {
            engine.GameFont = engine.Content.Load<SpriteFont>(value);
            engine.GameFontName = value;
        }

        private static void SetMeterValue(string value, Game1 engine)
        {
            try
            {
                float meter = (float)Convert.ToDouble(value);

                World.World.Meter = meter;
            }
            catch (Exception)
            {
                throw new DSSException(String.Format("'{0}' is not a valid FLOAT value for Meter.", value));
            }
        }

        private static void SetGravityValue(string value, Game1 engine)
        {
            try
            {
                float gravity = (float)Convert.ToDouble(value);

                World.World.Gravity = gravity;
            }
            catch (Exception)
            {
                throw new DSSException(String.Format("'{0}' is not a valid FLOAT value for Gravity.", value));
            }
        }

        #endregion
    }
}
